<template lang="">
    <div>
        <el-dialog :before-close="beforeClose" v-model="isShow" center :title="props.isAdd ? '添加角色' : '修改角色'">
    <el-form :model="userForm" :rules="rules" ref="ruleFormRef" label-width="120px" class="demo-ruleForm"
                :size="formSize" status-icon>
        <el-form-item label="所属角色" prop="roleid"  :label-width="formLabelWidth">
      <el-select v-model="userForm.roleid" placeholder="请选择上级菜单">
        <!-- <el-option label="" :value="0" /> -->
        <el-option v-for="item in rolelist" :key="item.id" :label="item.rolename" :value="item.id" ></el-option>
      </el-select>
    </el-form-item>         
      <el-form-item label="用户名称" prop="username" :label-width="formLabelWidth">
        <el-input v-model="userForm.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码"  :label-width="formLabelWidth">
        <el-input v-model="userForm.password" autocomplete="off" />
      </el-form-item>
     
      <el-form-item label="状态" :label-width="formLabelWidth">
              <el-switch v-model="userForm.status" active-color="#13ce66" inactive-color="#ff4949"
                  :active-value="1" :inactive-value="2" autocomplete="off"></el-switch>
              </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel(ruleFormRef)">取消</el-button>
        <el-button type="primary" @click="comfirm(ruleFormRef)" >确定</el-button>
      </span>
    </template>
  </el-dialog>
    </div>
</template>
<script lang="ts" setup>
import { getUseradd, getuserinfo, getuseredit } from '@/request/api'
import { reactive, ref, defineProps, defineEmits, computed, defineExpose } from 'vue'
import { FormRules, FormInstance, ElMessage } from 'element-plus'
// 引入仓库方法
import { useStore } from 'vuex';
import { useDialog } from 'element-plus/lib/components';
const ruleFormRef = ref<FormInstance>()
const store = useStore();
// 获取上级菜单列表
const rolelist = computed(() => store.getters["role/getMenu"])
console.log(rolelist, 'rolelist');
const getRole = () => {
  store.dispatch('role/getRoleAction')
}
getRole();
let props = defineProps({
  isShow: Boolean,
  isAdd: Boolean
})

const formLabelWidth = '140px'

const userForm = ref({
  username: '',// | 角色名称        | string         |
  password: '',//    | 角色权限 id数组 | '2,3,4' 字符串 |
  status: 1,//  | 状态1正常2禁用  | number         |
  roleid: ''
})
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '请输入菜单名称', trigger: 'blur' },
    { min: 2, max: 5, message: '菜单名称长度为2到5', trigger: 'blur' },
  ],
  roleid: [
    {
      required: true,
      message: ' 请选择上级菜单',
      trigger: 'change',
    },
  ],
})
// 封装一个beforeClose处理方法
const beforeClose = (done: () => {}) => {
  done();
  cancel(ruleFormRef.value)
}
// 取消事件
const emit = defineEmits(['cancel'])

// 表单清空事件
const cancel = (formEl: FormInstance | undefined) => {
  console.log(formEl, "formElformElformEl");
  emit("cancel", false);
  if (!formEl) return;
  formEl.resetFields();
  userForm.value = {
    username: '',// | 角色名称        | string         |
    password: '',//    | 角色权限 id数组 | '2,3,4' 字符串 |
    status: 1,//  | 状态1正常2禁用  | number         |
    roleid: ''
  };
};

// 获取一条数据
const lookup = (e: any) => {
  getuserinfo({ uid: e }).then(res => {
    console.log(res, '一条数据');
    if (res.code == 200) {
      userForm.value = res.list
      userForm.value.uid = e
       // 注意： 为空则不做修改！！！
       userForm.value.password = ""
    }
  })

}
defineExpose({
  lookup
})
// /添加事件
const comfirm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      ElMessage.success("验证成功");
      if (props.isAdd) {
        console.log(userForm, '表单提交');
        getUseradd(userForm.value).then(res => {
          console.log(res, '添加');
          if (res.code == 200) {
            ElMessage.success(res.msg);
            cancel(formEl)
            store.dispatch('user/changeUserAction')
            store.dispatch('user/getCountAction')
          } else {
            ElMessage.error(res.msg)
          }
        })
      } else {
        getuseredit(userForm.value).then(res => {
          console.log(res, '添加');
          if (res.code == 200) {
            ElMessage.success(res.msg);
            cancel(formEl)
            store.dispatch('user/changeUserAction')
          } else {
            ElMessage.error(res.msg)
          }
        })

      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
</script>
<style>
</style>